import re
import logging
from odoo import fields, api, models, tools, _
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)


class FactoryApiLog(models.Model):
    _name = 'factory.api.log'
    _description = 'Factory API Log'

    MESSAGE_TYPE = [
        ('qw_wechat', '企微通知'),
        ('sms', '短信通知'),
        ('wx', '小程序通知'),
    ]

    message_type = fields.Selection(MESSAGE_TYPE, '消息类型', default="qw_wechat")
    remark = fields.Char(string="备注")
    description = fields.Text(string="备注详情")
    send_body = fields.Text('发送体')
    resp_body = fields.Text('响应体')
    server_log = fields.Text('代码异常日志')
    result = fields.Boolean('通知结果',default=False)
    factory_visitor_order_id = fields.Many2one('factory.visitor')

    def re_push(self):
        if self.message_type == 'qw_wechat':
            return self.factory_visitor_order_id.wechat_notify(repush_obj=self, description=self.description, remark=self.remark)
        elif self.message_type == 'wx':
            return self.factory_visitor_order_id.wx_notify(repush_obj=self, remark=self.remark)
        else:
            return self.factory_visitor_order_id.sms_notify(repush_obj=self)

